package com.atguigu.tingshu.payment.context;

import com.atguigu.tingshu.payment.strategy.PaySubmitStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PaySubmitStrategyContext implements ApplicationContextAware {

    private static final Map<String, PaySubmitStrategy> PAY_SUBMIT_STRATEGY_MAP = new HashMap<>();

    public static PaySubmitStrategy getPaySubmitStrategy(String paymentType){
        return PAY_SUBMIT_STRATEGY_MAP.get(paymentType);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansOfType(PaySubmitStrategy.class).values().forEach(paySubmitStrategy -> {
            String paymentType = paySubmitStrategy.getpaymentType();
            PAY_SUBMIT_STRATEGY_MAP.put(paymentType, paySubmitStrategy);
        });
    }
}
